//
//  LLMyCardController.m
//  Weibo
//
//  Created by 李灵斌 on 15-1-31.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "LLMyCardController.h"

@interface LLMyCardController ()
@property (weak, nonatomic) IBOutlet UIImageView *qrcodeImage;

@end

@implementation LLMyCardController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:@"navigationbar_more" higImage:@"navigationbar_more_highlighted" action:nil target:nil];
    
    self.navigationItem.title = @"我的名片";

    [self.navigationController.navigationBar setBackgroundImage:[UIImage stretchableImageWithImage:@"navigationbar_background"] forBarMetrics:UIBarMetricsDefault];
    
    
    // 生成二维码
    [self setupQrcode];
}

// 生成二维码
- (void)setupQrcode{
    
    // 1,创建滤镜
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    // 2,还原滤镜默认的属性
    [filter setDefaults];
    // 3，将需要生成的二维码数据转换成二进制
    NSData *data = [@"www.weibo.com" dataUsingEncoding:NSUTF8StringEncoding];
    // 4, 给滤镜设置数据
    [filter setValue:data forKeyPath:@"inputMessage"];
    // 5，生成图片
    CIImage *qrcodeImage = [filter outputImage];
    
    UIImage *bg = [self createNonInterpolatedUIImageFormCIImage:qrcodeImage withSize:600];
//    UIImage *icon = [UIImage imageNamed:@"icon"];
    self.qrcodeImage.image = [self creteImageWithBg:bg icon:nil];
}

- (UIImage *)creteImageWithBg:(UIImage *)bg icon:(UIImage *)icon
{
    // 1.创建图形上下文
    UIGraphicsBeginImageContextWithOptions(bg.size, YES, 0.0);
    // 2.绘制背景
    [bg drawInRect:CGRectMake(0, 0, bg.size.width, bg.size.height)];
    // 3.绘制图标
    CGFloat iconW = 60;
    CGFloat iconH = 60;
    CGFloat iconX = (bg.size.width - iconW) * 0.5;
    CGFloat iconY = (bg.size.height - iconH) * 0.5;
    [icon drawInRect:CGRectMake(iconX, iconY, iconW, iconH)];
    // 4.取出图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    // 5.关闭上下文
    UIGraphicsEndImageContext();
    
    // 5.返回图片
    return newImage;
}

/**
 *  根据CIImage生成指定大小的UIImage
 *
 *  @param image CIImage
 *  @param size  图片宽度
 */
- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size
{
    CGRect extent = CGRectIntegral(image.extent);
    CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
    // 1.创建bitmap;
    size_t width = CGRectGetWidth(extent) * scale;
    size_t height = CGRectGetHeight(extent) * scale;
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    CGContextScaleCTM(bitmapRef, scale, scale);
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    // 2.保存bitmap到图片
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    return [UIImage imageWithCGImage:scaledImage];
}


@end
